<?php
/**
 * Cookie encoding and decoding class
 *
 * @package OWL
 * @subpackage Core
 * @author KeiMOon
 * @since 1.0
 */

class Owl_Cookie {

/**
 * Encode a cookie's name and value pair and send it to the client.
 *
 * @param string $name Name of the cookie
 * @param string $value Value of the cookie
 */
    static function set_cookie($name, $value) {
        if (headers_sent()) {
            return false;
        }
        $name_encode = "owl_".Owl_System_Helper::name_encode($name);
        $value_encode = base64_encode($value);
        return setcookie($name_encode, $value_encode, time() + COOKIE_LIFETIME, Owl_Functions::get_working_dir());
    }

    /**
     * Get and decode the cookie
     *
     * @param string $name Name of the cookie
     * @return string|bool
     */
    static function get_cookie($name) {
        $name_encode = "owl_".Owl_System_Helper::name_encode($name);
        if (isset($_COOKIE[$name_encode])) {
            return base64_decode($_COOKIE[$name_encode]);
        }
        else {
            return false;
        }
    }

    /**
     * Hashed an cookie and send it to the client
     *
     * @param string $name Name of the cookie
     * @param string $key Key of the cookie
     * @param string $value Value of the cookie
     * @param string $salt Salt of the cookie
     */
    static function set_hash_cookie($name, $key, $value, $salt) {
        if (headers_sent()) {
            return false;
        }
        $name_encode = "owl_".Owl_System_Helper::name_encode($name)."||".COOKIEHASH;
        $value_hashed = base64_encode($key)."||".Owl_System_Helper::hash($value, $salt);
        return setcookie($name_encode, $value_hashed, time() + COOKIE_LIFETIME, Owl_Functions::get_working_dir());
    }

    /**
     * Get the key of a cookie hash
     *
     * @param string $name Name of the cookie
     * @return string|bool Key of the cookie
     */
    static function get_hash_cookie_key($name) {
        $name_encode = "owl_".Owl_System_Helper::name_encode($name)."||".COOKIEHASH;
        if (isset($_COOKIE[$name_encode])) {
            $value = explode("||", $_COOKIE[$name_encode]);
            return base64_decode($value[0]);
        }
        else {
            return false;
        }
    }

    /**
     * Authenticate a cookie hash
     *
     * @param string $name Name of the cookie
     * @param sring $value Value to check
     * @param string $salt Salt to check
     * @return bool
     */
    static function authenticate_hash_cookie($name, $value, $salt) {
        $name_encode = "owl_".Owl_System_Helper::name_encode($name)."||".COOKIEHASH;
        if (isset($_COOKIE[$name_encode])) {
            $cookie_value = explode("||", $_COOKIE[$name_encode]);
            return ($cookie_value[1] == Owl_System_Helper::hash($value, $salt));
        }
        else {
            return false;
        }
    }

    /**
     * Delete a cookie by its name
     *
     * @param string $name
     */
    static function delete_cookie($name) {
        if (self::get_cookie($name) !== false) {
            $name_encode = "owl_".Owl_System_Helper::name_encode($name);
            setcookie($name_encode, "", time() - 3600, SITE_URL);
        }
    }

    /**
     * Delete a hashed cookie by its name
     *
     * @param string $name
     */
    static function delete_hashed_cookie($name) {
        $name_encode = "owl_".Owl_System_Helper::name_encode($name)."||".COOKIEHASH;
        setcookie($name_encode, "", time() - 3600, SITE_URL);
    }
}


